import java.util.*;

public class Solution {

    //
    public int minSubArrayLen(int target, int[] nums) {
        int l = 0, r = 0, len = 0x3f3f3f3f, sum = 0, n = nums.length;
        while(r < n){
            sum += nums[r];
            while(sum >= target){
                len = Math.min(len, r - l + 1);
                sum -= nums[l++];
            }
            r++;
        }
        return len == 0x3f3f3f3f ? 0 : len;
    }

    //
    public int lengthOfLongestSubstring(String s) {
        int l = 0, r = 0, n = s.length(), len = 0;
        int[] hash = new int[128];

        while(r < n){
            hash[s.charAt(r)]++;

            while(hash[s.charAt(r)] > 1){
                hash[s.charAt(l++)]--;
            }
            len = Math.max(len, r - l + 1);
            r++;
        }
        return len;
    }

    //
    public int longestOnes(int[] nums, int k) {
        int l = 0, r = 0, one = 0, zero = 0, ret = 0, n = nums.length;
        while(r < n){
            if(nums[r] == 0){
                zero++;
                one++;
            }else{
                one++;
            }

            while(zero > k){
                if(nums[l] == 0){
                    zero--;
                    one--;
                }else{
                    one--;
                }
                l++;
            }

            ret = Math.max(ret, one);
            r++;
        }
        return ret;
    }

    //
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println(i);
        }
        System.out.println();
        for (int i = 0; i < 3; ++i) {
            System.out.println(i);
        }
    }

    //
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        for(int t : nums){
            sum += t;
        }
        sum -= x;
        if(sum < 0) { return -1;}

        int l = 0, r = 0, n = nums.length, ret = -1;
        while(r < n){
            sum -= nums[r];

            while(sum < 0){
                sum += nums[l++];
            }
            if(sum == 0) { ret = Math.max(ret, r - l + 1);}
            r++;
        }
        return ret == -1 ? -1 : n - ret;
    }

    //
    public int totalFruit(int[] fruits) {
        int l = 0, r = 0, n = fruits.length, kinds = 0, ret = 0;
        int[] hash = new int[n];

        while(r < n){
            if(hash[fruits[r]]++ == 0) { kinds++;}

            while(kinds > 2){
                if(hash[fruits[l++]]-- == 1) { kinds--;}
            }
            ret = Math.max(ret, r - l + 1);
            r++;
        }
        return ret;
    }

    //
    public List<Integer> findAnagrams(String _s, String _p) {
        List<Integer> ans = new ArrayList<>();
        char[] s = _s.toCharArray();
        char[] p = _p.toCharArray();

        int[] hashP = new int[26];
        int n = p.length;
        for(char x : p){
            hashP[x - 'a']++;
        }

        int[] hashS = new int[26];
        int m = s.length, count = 0;
        for(int l = 0, r = 0; r < m; r++){
            int in = s[r] - 'a';
            if(hashP[in] >= ++hashS[in]) { count++;}

            while(r - l + 1 > n){
                int out = s[l++] - 'a';
                if(hashP[out] >= hashS[out]--) { count--;}
            }

            if(count == n) { ans.add(l);}
        }
        return ans;
    }

    //
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> ans = new ArrayList<>();
        Map<String, Integer> hash1 = new HashMap<>();
        for(String x : words){
            hash1.put(x, hash1.getOrDefault(x, 0) + 1);
        }

        int m = words.length, n = words[0].length();
        for(int i = 0; i < n; i++){
            Map<String, Integer> hash2 = new HashMap<>();
            for(int l = i, r = i, count = 0; r + n <= s.length(); r += n){
                String in = s.substring(r, r + n);
                hash2.put(in, hash2.getOrDefault(in, 0) + 1);
                if(hash1.getOrDefault(in, 0) >= hash2.get(in)) { count++;}

                while(r - l + 1 > n * m){
                    String out = s.substring(l, l + n);
                    if(hash1.getOrDefault(out, 0) >= hash2.get(out)) { count--;}
                    hash2.put(out, hash2.get(out) - 1);
                    l += n;
                }
                if(count == m) { ans.add(l);}
            }
        }
        return ans;
    }

    //
    public String minWindow(String _s, String _t) {
        char[] s = _s.toCharArray();
        char[] t = _t.toCharArray();

        int[] hashT = new int[128];
        int kinds = 0;
        for(char ch : t){
            if(hashT[ch]++ == 0) { kinds++;}
        }

        int begin = -1, min = Integer.MAX_VALUE;
        int[] hashS = new int[128];
        for(int l = 0, r = 0, count = 0; r < s.length; r++){
            char in = s[r];
            if(hashT[in] == ++hashS[in]) {count++;}

            while(count == kinds){
                if(min > r - l + 1){
                    min = r - l + 1;
                    begin = l;
                }

                char out = s[l++];
                if(hashT[out] == hashS[out]--) { count--;}
            }
        }

        return begin == -1 ? "" : _s.substring(begin, begin + min);
    }

    //
    public int search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while(l <= r){
            int mid = l + (r - l) / 2;
            if(nums[mid] > target){
                r = mid - 1;
            }else if(nums[mid] < target){
                l = mid + 1;
            }else{
                return mid;
            }
        }
        return -1;
    }

    //
    public int[] searchRange(int[] nums, int target) {
        int[] ans = new int[2];
        ans[0] = ans[1] = -1;
        if(nums.length == 0) { return ans;}

        int l = 0, r = nums.length - 1;
        while(l < r){
            int mid = l + (r - l) / 2;
            if(nums[mid] < target){
                l = mid + 1;
            }else{
                r = mid;
            }
        }
        if(nums[l] != target) { return ans;}
        ans[0] = l;

        l = 0; r = nums.length - 1;
        while(l < r){
            int mid = l + (r - l + 1) / 2;
            if(nums[mid] > target){
                r = mid - 1;
            }else{
                l = mid;
            }
        }
        ans[1] = l;

        return ans;
    }

    //
}
